package org.dromara.cms.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.cms.domain.bo.CmsArticleBo;
import org.dromara.cms.domain.vo.CmsArticleVo;
import org.dromara.cms.domain.CmsArticle;
import org.dromara.cms.mapper.CmsArticleMapper;
import org.dromara.cms.service.ICmsArticleService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * CMS文章Service业务层处理
 *
 * @author Spring
 * @date 2024-12-25
 */
@RequiredArgsConstructor
@Service
public class CmsArticleServiceImpl implements ICmsArticleService {

    private final CmsArticleMapper baseMapper;

    /**
     * 查询CMS文章
     *
     * @param id 主键
     * @return CMS文章
     */
    @Override
    public CmsArticleVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询CMS文章列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return CMS文章分页列表
     */
    @Override
    public TableDataInfo<CmsArticleVo> queryPageList(CmsArticleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CmsArticle> lqw = buildQueryWrapper(bo);
        Page<CmsArticleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的CMS文章列表
     *
     * @param bo 查询条件
     * @return CMS文章列表
     */
    @Override
    public List<CmsArticleVo> queryList(CmsArticleBo bo) {
        LambdaQueryWrapper<CmsArticle> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CmsArticle> buildQueryWrapper(CmsArticleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CmsArticle> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSlug()), CmsArticle::getSlug, bo.getSlug());
        lqw.eq(bo.getCategoryId() != null, CmsArticle::getCategoryId, bo.getCategoryId());
        lqw.eq(StringUtils.isNotBlank(bo.getAuthor()), CmsArticle::getAuthor, bo.getAuthor());
        lqw.eq(bo.getPublishTime() != null, CmsArticle::getPublishTime, bo.getPublishTime());
        lqw.eq(bo.getIsTop() != null, CmsArticle::getIsTop, bo.getIsTop());
        lqw.eq(StringUtils.isNotBlank(bo.getSeoTitle()), CmsArticle::getSeoTitle, bo.getSeoTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getSeoKeywords()), CmsArticle::getSeoKeywords, bo.getSeoKeywords());
        lqw.eq(StringUtils.isNotBlank(bo.getSeoDescription()), CmsArticle::getSeoDescription, bo.getSeoDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), CmsArticle::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getTemplate()), CmsArticle::getTemplate, bo.getTemplate());
        lqw.eq(StringUtils.isNotBlank(bo.getSource()), CmsArticle::getSource, bo.getSource());
        lqw.eq(StringUtils.isNotBlank(bo.getSourceUrl()), CmsArticle::getSourceUrl, bo.getSourceUrl());
        lqw.eq(bo.getAllowComment() != null, CmsArticle::getAllowComment, bo.getAllowComment());
        lqw.eq(bo.getSort() != null, CmsArticle::getSort, bo.getSort());
        return lqw;
    }

    /**
     * 新增CMS文章
     *
     * @param bo CMS文章
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(CmsArticleBo bo) {
        CmsArticle add = MapstructUtils.convert(bo, CmsArticle.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改CMS文章
     *
     * @param bo CMS文章
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(CmsArticleBo bo) {
        CmsArticle update = MapstructUtils.convert(bo, CmsArticle.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CmsArticle entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除CMS文章信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
